<template>
  <!-- 页面主题区域 -->
  <el-container>
    <!-- 左边侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <!-- 左边侧边菜单栏 -->
      <el-menu background-color="#293846" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse="isCollapse" :collapse-transition="isTransition"
               router :default-active="currentPath">
        <!-- 一级菜单 -->
        <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
          <!-- 一级菜单模板区域 -->
          <template slot="title">
            <!-- 图标 -->
            <i :class="iconsObj[item.id]"></i>
            <!-- 文本 -->
            <span>{{ item.authName }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id"
                        @click="saveNavState(subItem.path)">
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <el-button size="mini" class="toggle-button" type="success" icon="el-icon-s-fold" @click="toggleCollapse"></el-button>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 右侧主题区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
        '146': 'iconfont icon-work',
      },
      isCollapse: false,
      isTransition: false,
      //被激活的链接
      currentPath: ''
    }
  },

  created() {
    this.getMenuList()
    this.currentPath = window.sessionStorage.getItem('currentPath')
  },

  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {

      const { data: resp } = await this.$http.get('menus')
     console.log(resp.data)
      if (resp.status !== '200') return this.$message.error(resp.msg)
      this.menuList = resp.data
    },
    // 点击按钮折叠左侧菜单
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //保存链接的激活状态
    saveNavState(currentPath) {
      window.sessionStorage.setItem('currentPath', currentPath)
      this.currentPath = currentPath
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #FFF;
  display: flex;
  justify-content: space-between;//左右贴边对其
  align-items: center;
  border-bottom:1px solid #DDD;
  // color: #fff;
  .toggle-button {
    cursor: pointer;
  }
}

.el-aside {
  background-color: #2f4050;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #ffffff;
}

.iconfont {
  margin-right: 8px;
}
</style>
